package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.PlanProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProjects;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.PlanProjectVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ykeezy
 * @version 1.0
 * @date 2024/9/3 17:52
 * @description TODO
 */
@Service
@AllArgsConstructor
public class NursingPlanServiceImpl implements NursingPlanService {
    private NursingPlanMapper nursingPlanMapper;
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    /**
     * 获取所有护理计划信息
     */
    @Override
    public List<NursingPlanVo> getAllPlans() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectAll();
        List<NursingPlanVo> npvList = nursingPlans.stream().map(nlItem -> BeanUtil.toBean(nlItem, NursingPlanVo.class)).toList();
        return npvList;
    }

    /**
     * 新增护理计划
     */
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        Long planId = nursingPlan.getId();
        for (PlanProjectDto planProjectDto : nursingPlanDto.getProjectPlans()) {
            NursingPlanProjects nursingPlanProjects = BeanUtil.toBean(planProjectDto, NursingPlanProjects.class);
            nursingPlanProjects.setPlanId(planId);
            nursingPlanMapper.insertNursingPlanProjects(nursingPlanProjects);
        }
    }

    /**
     * 分页查询
     */
    @Override
    public PageResponse<NursingPlanVo> search(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> pageInfo = nursingPlanMapper.search(name, status);
        return PageResponse.of(pageInfo, NursingPlanVo.class);
    }

    /**
     * 根据ID查询护理计划
     */
    @Override
    public NursingPlanVo getPlanById(Long id) {
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlanMapper.getPlanById(id), NursingPlanVo.class);
        List<NursingPlanProjects> nursingPlanProjects = nursingPlanMapper.getProjectsByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingPlanProjects);
        return nursingPlanVo;

    }

    @Transactional
    @Override
    public ResponseResult updateById(NursingPlanDto nursingPlanDto, Long id) {
        if (id == null){
            throw new BaseException(BasicEnum.ID_IS_NULL);

        }

        if (nursingPlanMapper.isBindLevel(id) != 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_WAS_BIND);

        }

        //DTO -> Entity
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setPlanName(nursingPlanDto.getPlanName());
        List<NursingPlanProjects> nppList = nursingPlanDto.getProjectPlans().stream()
                .map(planProjectDto -> BeanUtil.toBean(planProjectDto, NursingPlanProjects.class)).collect(Collectors.toList());
        nursingPlan.setNursingPlanProjects(nppList);

        //先更新护理计划表
        nursingPlan.setId(id);
        Integer outcome = nursingPlanMapper.updateById(nursingPlan);
        if (outcome == 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_UPDATE_FAIL);

        }

        //先删除相关项，再插入相关项
        nursingPlanProjectMapper.deletePlanRelativeItems(id);
        //再更新关联表
        if (nursingPlanProjectMapper.insertItemsWhenPlanUpdate(nursingPlan.getNursingPlanProjects(), nursingPlan.getId()) == 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_UPDATE_FAIL);

        }

        return ResponseResult.success();

    }

    @Transactional
    @Override
    public ResponseResult deleteById(Long id) {
        if (id == null){
            throw new BaseException(BasicEnum.ID_IS_NULL);

        }

        if (nursingPlanMapper.isBindLevel(id) != 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_WAS_BIND);

        }

        //删除护理计划表
        if (nursingPlanMapper.deleteById(id) == 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL);

        }

        //删除关联表项
        nursingPlanProjectMapper.deletePlanRelativeItems(id);
        return ResponseResult.success();

    }

    @Transactional
    @Override
    public ResponseResult enableOrDisable(Long id, Integer status) {
        if (id == null || status == null){
            throw new BaseException(BasicEnum.ID_OR_STATUS_IS_NULL);

        }

        if (nursingPlanMapper.isBindLevel(id) != 0){
            throw new BaseException(BasicEnum.NURSING_PLAN_WAS_BIND);

        }

        //增加状态值范围校验
        if (status != 0 && status != 1){
            throw new BaseException(BasicEnum.INVALID_STATUS_VALUE);

        }

        //查看是否启用或禁用成功
        if (nursingPlanMapper.enableOrDisablePlan(id, status) == 0){
            throw new BaseException(BasicEnum.ENABLE_OR_DISABLE_FAIL);

        }

        return ResponseResult.success();

    }
}
